/*
 * FaceCat图形通讯框架
 * 著作权编号:2015SR229355+2020SR0266727
 * 上海卷卷猫信息技术有限公司
 */

using System;
using System.Collections.Generic;

namespace FaceCat {
    /// <summary>
    /// 分割布局视图
    /// </summary>
    public class FCSplitLayoutDiv : FCLayoutDiv {

        /// <summary>
        /// 调整大小的类型
        /// </summary>
        public enum FCSizeType
        {
            /// <summary>
            /// 绝对大小
            /// </summary>
            AbsoluteSize,
            /// <summary>
            /// 自动填充
            /// </summary>
            AutoFill,
            /// <summary>
            /// 百分比大小
            /// </summary>
            PercentSize
        }

        /// <summary>
        /// 创建分割布局视图
        /// </summary>
        public FCSplitLayoutDiv() {
        }

        /// <summary>
        /// 分割百分比
        /// </summary>
        public float m_splitPercent = -1;

        public FCSize m_datumSize = new FCSize();

        /// <summary>
        /// 获取基准尺寸
        /// </summary>
        public virtual FCSize getDatumSize() {
            return m_datumSize;
        }

        /// <summary>
        /// 设置基准尺寸
        /// </summary>
        public virtual void setDatumSize(FCSize datumSize) {
            m_datumSize = datumSize;
        }

        public FCView m_firstView;

        /// <summary>
        /// 获取第一个视图
        /// </summary>
        public virtual FCView getFirstView() {
            return m_firstView;
        }

        /// <summary>
        /// 设置第一个视图
        /// </summary>
        public virtual void setFirstView(FCView value) {
            m_firstView = value;
        }

        public FCView m_secondView;

        /// <summary>
        /// 获取第二个视图
        /// </summary>
        public virtual FCView getSecondView() {
            return m_secondView;
        }

        /// <summary>
        /// 设置第二个视图
        /// </summary>
        public virtual void setSecondView(FCView value) {
            m_secondView = value;
        }

        public FCSizeType m_splitMode = FCSizeType.AbsoluteSize;

        /// <summary>
        /// 获取分割模式
        /// </summary>
        public virtual FCSizeType getSplitMode() {
            return m_splitMode;
        }

        /// <summary>
        /// 设置分割模式
        /// </summary>
        public virtual void setSplitMode(FCSizeType value) {
            m_splitMode = value;
        }

        public FCButton m_splitter;

        /// <summary>
        /// 获取分割按钮
        /// </summary>
        public virtual FCButton getSplitter() {
            return m_splitter;
        }

        /// <summary>
        /// 触发事件
        /// </summary>
        /// <param name="eventName"></param>
        /// <param name="sender"></param>
        /// <param name="invoke"></param>
        public override void callEvent(String eventName, Object sender, Object invoke) {
            base.callEvent(eventName, sender, invoke);
            if (eventName == "ondragging" && sender == m_splitter) {
                m_splitPercent = -1;
                update();
                invalidate();
            }
        }

        /// <summary>
        /// 销毁方法
        /// </summary>
        public override void delete() {
            if (!isDeleted()) {
                if (m_splitter != null) {
                    m_splitter.removeEvent(this, "ondragging");
                }
            }
            base.delete();
        }

        /// <summary>
        /// 获取视图类型
        /// </summary>
        /// <returns>视图类型</returns>
        public override String getViewType() {
            return "SplitLayoutDiv";
        }

        /// <summary>
        /// 获取属性值
        /// </summary>
        /// <param name="name">属性名称</param>
        /// <param name="value">返回属性值</param>
        /// <param name="type">返回属性类型</param>
        public override void getAttribute(String name, ref String value, ref String type) {
            switch (name)
            {
                case "candragsplitter":
                    {
                        type = "bool";
                        if (m_splitter != null)
                        {
                            value = FCTran.boolToStr(m_splitter.allowDrag());
                        }
                        else
                        {
                            value = "False";
                        }
                        break;
                    }
                case "datumsize":
                    type = "size";
                    value = FCTran.sizeToStr(getDatumSize());
                    break;
                case "splitmode":
                    {
                        type = "enum:FCSizeType";
                        if (getSplitMode() == FCSizeType.AbsoluteSize)
                        {
                            value = "AbsoluteSize";
                        }
                        else
                        {
                            value = "PercentSize";
                        }
                        break;
                    }
                case "splitterposition":
                    {
                        type = "str";
                        if (m_splitter != null)
                        {
                            if (m_layoutStyle == FCLayoutStyle.TopToBottom || m_layoutStyle == FCLayoutStyle.BottomToTop)
                            {
                                value = FCTran.intToStr(m_splitter.getTop());
                                if (m_splitter.getHeight() > 0)
                                {
                                    value = value + "," + FCTran.intToStr(m_splitter.getHeight());
                                }
                            }
                            else
                            {
                                value = FCTran.intToStr(m_splitter.getLeft());
                                if (m_splitter.getWidth() > 0)
                                {
                                    value = value + "," + FCTran.intToStr(m_splitter.getWidth());
                                }
                            }
                        }
                        break;
                    }
                case "splittervisible":
                    {
                        type = "bool";
                        if (m_splitter != null)
                        {
                            value = FCTran.boolToStr(m_splitter.isVisible());
                        }
                        else
                        {
                            value = "False";
                        }
                        break;
                    }
                default:
                    if (name.IndexOf("splitter-") != -1)
                    {
                        if (m_splitter != null)
                        {
                            m_splitter.getAttribute(name.Substring(9), ref value, ref type);
                        }
                    }
                    else
                    {
                        base.getAttribute(name, ref value, ref type);
                    }
                    break;
            }
        }

        /// <summary>
        /// 获取属性名称列表
        /// </summary>
        /// <returns>属性名称列表</returns>
        public override ArrayList<String> getAttributeNames() {
            ArrayList<String> attributeNames = base.getAttributeNames();
            attributeNames.AddRange(new String[] { "CanDragSplitter", "DatumSize", "SplitMode", "SplitterPosition", "SplitterVisible" });
            return attributeNames;
        }

        /// <summary>
        /// 添加视图方法
        /// </summary>
        public override void onLoad() {
            base.onLoad();
            if (m_splitter == null) {
                FCHost host = getNative().getHost();
                m_splitter = host.createInternalView(this, "splitter") as FCButton;
                m_splitter.addEvent(this, "ondragging", null);
                addView(m_splitter);
            }
            m_oldSize = getSize();
        }

        /// <summary>
        /// 重置布局
        /// </summary>
        /// <returns></returns>
        public override bool onResetLayout() {
            bool reset = false;
            if (getNative() != null && m_splitter != null && m_firstView != null && m_secondView != null) {
                FCRect splitRect = new FCRect();
                int width = getWidth(), height = getHeight();
                FCRect fRect = new FCRect();
                FCRect sRect = new FCRect();
                FCSize splitterSize = new FCSize(0, 0);
                if (m_splitter.isVisible()) {
                    splitterSize.cx = m_splitter.getWidth();
                    splitterSize.cy = m_splitter.getHeight();
                }
                FCLayoutStyle layoutStyle = getLayoutStyle();
                switch (layoutStyle) {
                    //自下而上
                    case FCLayoutStyle.BottomToTop:
                        //绝对大小
                        if (m_splitMode == FCSizeType.AbsoluteSize || m_oldSize.cy == 0) {
                            splitRect.left = 0;
                            splitRect.top = height - (m_oldSize.cy - m_splitter.getTop());
                            splitRect.right = width;
                            splitRect.bottom = splitRect.top + splitterSize.cy;
                        }
                            //百分比大小
                        else if (m_splitMode == FCSizeType.PercentSize) {
                            splitRect.left = 0;
                            if (m_splitPercent == -1) {
                                m_splitPercent = (float)m_splitter.getTop() / m_oldSize.cy;
                            }
                            splitRect.top = (int)(height * m_splitPercent);
                            splitRect.right = width;
                            splitRect.bottom = splitRect.top + splitterSize.cy;
                        }
                        fRect.left = 0;
                        fRect.top = splitRect.bottom;
                        fRect.right = width;
                        fRect.bottom = height;
                        sRect.left = 0;
                        sRect.top = 0;
                        sRect.right = width;
                        sRect.bottom = splitRect.top;
                        break;
                    //从左向右
                    case FCLayoutStyle.LeftToRight:
                        //绝对大小
                        if (m_splitMode == FCSizeType.AbsoluteSize || m_oldSize.cx == 0) {
                            splitRect.left = m_splitter.getLeft();
                            splitRect.top = 0;
                            splitRect.right = m_splitter.getLeft() + splitterSize.cx;
                            splitRect.bottom = height;
                        }
                            //百分比大小
                        else if (m_splitMode == FCSizeType.PercentSize) {
                            if (m_splitPercent == -1) {
                                m_splitPercent = (float)m_splitter.getLeft() / m_oldSize.cx;
                            }
                            splitRect.left = (int)(width * m_splitPercent);
                            splitRect.top = 0;
                            splitRect.right = splitRect.left + splitterSize.cx;
                            splitRect.bottom = height;
                        }
                        fRect.left = 0;
                        fRect.top = 0;
                        fRect.right = splitRect.left;
                        fRect.bottom = height;
                        sRect.left = splitRect.right;
                        sRect.top = 0;
                        sRect.right = width;
                        sRect.bottom = height;
                        break;
                    //从右向左
                    case FCLayoutStyle.RightToLeft:
                        //绝对大小
                        if (m_splitMode == FCSizeType.AbsoluteSize || m_oldSize.cx == 0) {
                            splitRect.left = width - (m_oldSize.cx - m_splitter.getLeft());
                            splitRect.top = 0;
                            splitRect.right = splitRect.left + splitterSize.cx;
                            splitRect.bottom = height;
                        }
                            //百分比大小
                        else if (m_splitMode == FCSizeType.PercentSize) {
                            if (m_splitPercent == -1) {
                                m_splitPercent = (float)m_splitter.getLeft() / m_oldSize.cx;
                            }
                            splitRect.left = (int)(width * m_splitPercent);
                            splitRect.top = 0;
                            splitRect.right = splitRect.left + splitterSize.cx;
                            splitRect.bottom = height;
                        }
                        fRect.left = splitRect.right;
                        fRect.top = 0;
                        fRect.right = width;
                        fRect.bottom = height;
                        sRect.left = 0;
                        sRect.top = 0;
                        sRect.right = splitRect.left;
                        sRect.bottom = height;
                        break;
                    //自上而下
                    case FCLayoutStyle.TopToBottom:
                        //绝对大小
                        if (m_splitMode == FCSizeType.AbsoluteSize || m_oldSize.cy == 0) {
                            splitRect.left = 0;
                            splitRect.top = m_splitter.getTop();
                            splitRect.right = width;
                            splitRect.bottom = splitRect.top + splitterSize.cy;
                        }
                            //百分比大小
                        else if (m_splitMode == FCSizeType.PercentSize) {
                            splitRect.left = 0;
                            if (m_splitPercent == -1) {
                                m_splitPercent = (float)m_splitter.getTop() / m_oldSize.cy;
                            }
                            splitRect.top = (int)(height * m_splitPercent);
                            splitRect.right = width;
                            splitRect.bottom = splitRect.top + splitterSize.cy;
                        }
                        fRect.left = 0;
                        fRect.top = 0;
                        fRect.right = width;
                        fRect.bottom = splitRect.top;
                        sRect.left = 0;
                        sRect.top = splitRect.bottom;
                        sRect.right = width;
                        sRect.bottom = height;
                        break;
                }
                if (m_splitter.isVisible()) {
                    FCRect spRect = m_splitter.getBounds();
                    if (spRect.left != splitRect.left || spRect.top != splitRect.top || spRect.right != splitRect.right || spRect.bottom != splitRect.bottom) {
                        m_splitter.setBounds(splitRect);
                        reset = true;
                    }
                    if (m_splitter.allowDrag()) {
                        if (layoutStyle == FCLayoutStyle.LeftToRight || layoutStyle == FCLayoutStyle.RightToLeft) {
                            m_splitter.setCursor(FCCursors.SizeWE);
                        } else {
                            m_splitter.setCursor(FCCursors.SizeNS);
                        }
                        m_splitter.bringToFront();
                    }
                }
                FCRect fcRect = m_firstView.getBounds();
                if (fcRect.left != fRect.left || fcRect.top != fRect.top || fcRect.right != fRect.right || fcRect.bottom != fRect.bottom) {
                    reset = true;
                    m_firstView.setBounds(fRect);
                    m_firstView.update();
                }
                FCRect scRect = m_secondView.getBounds();
                if (scRect.left != sRect.left || scRect.top != sRect.top || scRect.right != sRect.right || scRect.bottom != sRect.bottom) {
                    reset = true;
                    m_secondView.setBounds(sRect);
                    m_secondView.update();
                }
            }
            m_oldSize = getSize();
            return reset;
        }

        /// <summary>
        /// 设置属性
        /// </summary>
        /// <param name="name">属性名称</param>
        /// <param name="value">属性值</param>
        public override void setAttribute(String name, String value)
        {
            switch (name)
            {
                case "candragsplitter":
                    {
                        if (m_splitter != null)
                        {
                            m_splitter.setAllowDrag(FCTran.strToBool(value));
                        }
                        break;
                    }
                case "datumsize":
                    m_datumSize = FCTran.strToSize(value);
                    break;
                case "splitmode":
                    {
                        value = value.ToLower();
                        if (value == "absolutesize")
                        {
                            setSplitMode(FCSizeType.AbsoluteSize);
                        }
                        else
                        {
                            setSplitMode(FCSizeType.PercentSize);
                        }
                        break;
                    }
                case "splitterposition":
                    {
                        if (m_splitter != null)
                        {
                            String[] strs = value.Split(new String[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                            if (strs.Length == 4)
                            {
                                m_splitter.setBounds(FCTran.strToRect(value));
                            }
                            else if (strs.Length <= 2)
                            {
                                int pos = FCTran.strToInt(strs[0]);
                                int lWidth = 0;
                                if (strs.Length == 2)
                                {
                                    lWidth = FCTran.strToInt(strs[1]);
                                }
                                int width = getWidth(), height = getHeight();
                                if (m_datumSize.cx > 0 && m_datumSize.cy > 0)
                                {
                                    m_oldSize = m_datumSize;
                                }
                                m_splitPercent = -1;
                                if (m_layoutStyle == FCLayoutStyle.TopToBottom || m_layoutStyle == FCLayoutStyle.BottomToTop)
                                {
                                    m_splitter.setBounds(new FCRect(0, pos, width, pos + lWidth));
                                }
                                else
                                {
                                    m_splitter.setBounds(new FCRect(pos, 0, pos + lWidth, height));
                                }
                            }
                        }
                        break;
                    }
                case "splittervisible":
                    {
                        if (m_splitter != null)
                        {
                            m_splitter.setVisible(FCTran.strToBool(value));
                        }
                        break;
                    }
                default:
                    if (name.IndexOf("splitter-") != -1)
                    {
                        if (m_splitter != null)
                        {
                            m_splitter.setAttribute(name.Substring(9), value);
                        }
                    }
                    else
                    {
                        base.setAttribute(name, value);
                    }
                    break;
            }
        }

        /// <summary>
        /// 布局更新方法
        /// </summary>
        public override void update() {
            if (m_native != null && !m_native.m_lockUpdate)
            {
                onResetLayout();
                int viewsSize = m_views.size();
                for (int i = 0; i < viewsSize; i++)
                {
                    m_views.get(i).update();
                }
            }
        }
    }
}
